package com.example.sacletextview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.TextView;

public class SacleTextView extends TextView {
	/** 放大倍数1.2为120% */
	private static final float SCALE = 1.2f;
	/** 动画持续时间,单位:毫秒 */
	private static final long DURA = 200;

	public SacleTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SacleTextView(Context context) {
		this(context, null);
	}

	@Override
	public void setSelected(boolean selected) {
		if (isSelected() != selected) {
			ScaleAnimation anim = null;
			if (selected) {
				anim = getScaleAnim(1f, SCALE); // 放大
			} else {
				anim = getScaleAnim(SCALE, 1f); // 还原
			}
			startAnimation(anim);
		}
		super.setSelected(selected);
	}

	private ScaleAnimation getScaleAnim(float start, float dest) {
		ScaleAnimation anim = new ScaleAnimation(start, dest, start, dest,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		anim.setDuration(DURA);
		anim.setFillAfter(true);
		return anim;
	}

	// ---------------------非重要-------------
	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, widthMeasureSpec);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, w, oldw, oldh);
	}
	// ---------------------非重要-------------
}
